package kr.ac.hansung.www;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.ArrayList;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class SocketThread extends Thread {

	static final int UPDATE_EVENT = 0;
	static final int NEW_EVENT = 1;
	static final int JOIN_EVENT = 2;
	static final int UP_GPS = 3;
	static final int DOWN_LOG = 4;
	static final int UPDATE_MEMBER = 5;
	static final int UPDATE_MEMBER_LOCATION = 6;

	static final String TAG = "ConnectThread";

	private String HOST = "1.234.20.87";
	private int PORT = 1233;

	private Handler mHandler;
	private Object obj1;
	private Object obj2;
	private Object result;
	private int ctrlMsg;

	public SocketThread(Handler handler, Object obj1, Object obj2, int ctrlMsg) {
		mHandler = handler;
		this.obj1 = obj1;
		this.obj2 = obj2;
		this.ctrlMsg = ctrlMsg;
	}
	
	public void run() {

		switch (ctrlMsg) {

		case NEW_EVENT:
			sendNewEvent((Event) obj1);
			break;

		case UPDATE_EVENT:
			result = (Object) receiveAllEvent();
			break;

		case JOIN_EVENT:
			joinEvent((Event) obj1, (String) obj2);
			return;

		case UP_GPS:
			sendMyLocation((MyLocation) obj1);
			return;

		case UPDATE_MEMBER:
			result = (Object) receiveMemberList((Event) obj1);
			break;

		case DOWN_LOG:
			result = (Object) receiveMemberGPSLog((Event) obj1, (Member) obj2);
			break;
			
		case UPDATE_MEMBER_LOCATION:
			result = (Object) receiveMemberLocation((Event) obj1);
			break;
		}

		Message msg = mHandler.obtainMessage(ctrlMsg, result);
		mHandler.sendMessage(msg);
	}

	@SuppressWarnings("unchecked")
	public ArrayList<Event> receiveAllEvent() {
		Log.i(TAG, "receiveAllEvent");

		ArrayList<Event> eventList = new ArrayList<Event>();

		try {
			Socket socket = new Socket(HOST, PORT);

			ObjectOutputStream outStream = new ObjectOutputStream(
					socket.getOutputStream());
			outStream.writeObject("RequestEventlist/");

			outStream.flush();

			ObjectInputStream instream = new ObjectInputStream(
					socket.getInputStream());
			eventList = (ArrayList<Event>) instream.readObject();

			socket.close();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}

		return eventList;
	}

	public void sendNewEvent(Event newEvent) {
		Log.i(TAG, "sendNewEvent");
		try {
			Socket socket = new Socket(HOST, PORT);

			ObjectOutputStream outStream = new ObjectOutputStream(
					socket.getOutputStream());
			outStream.writeObject("SendEvent/");
			outStream.flush();

			outStream.writeObject(newEvent);
			outStream.flush();

			socket.close();

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void joinEvent(Event event, String myId) {

		try {
			Socket socket = new Socket(HOST, PORT);
			ObjectOutputStream outStream = new ObjectOutputStream(
					socket.getOutputStream());

			outStream.writeObject("EventJoin/" + myId);
			outStream.flush();
			
			outStream.writeObject(event);
			outStream.flush();

			socket.close();

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void sendMyLocation(MyLocation myLocation) {
		try {
			Socket socket = new Socket(HOST, PORT);

			ObjectOutputStream outStream = new ObjectOutputStream(
					socket.getOutputStream());
			outStream.writeObject("SendLocation/");
			outStream.flush();
			outStream.writeObject(myLocation);
			outStream.flush();

			socket.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	
	@SuppressWarnings("unchecked")
	public ArrayList<Member> receiveMemberList(Event currentEvent) {

		ArrayList<Member> memberList = null;
				
		try {

			Socket socket = new Socket(HOST, PORT);

			ObjectOutputStream outstream = new ObjectOutputStream(
					socket.getOutputStream());

			outstream.writeObject("RequestMemberList/");
			outstream.flush();

			outstream.writeObject(currentEvent);
			outstream.flush();
			
			ObjectInputStream instream = new ObjectInputStream(
					socket.getInputStream());
			memberList = (ArrayList<Member>) instream.readObject();

			socket.close();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}

		return memberList;
	}
	
	
	@SuppressWarnings("unchecked")
	public GPSManager receiveMemberGPSLog(Event currentEvent, Member selectMember) {

		GPSManager gpsManager = null;

		try {
			Socket socket = new Socket(HOST, PORT);

			ObjectOutputStream outStream = new ObjectOutputStream(
					socket.getOutputStream());
			outStream.writeObject("RequestTrack/");
			outStream.flush();

			outStream.writeObject(currentEvent);
			outStream.flush();
			
			outStream.writeObject(selectMember);
			outStream.flush();

			ObjectInputStream inputStream = new ObjectInputStream(
					socket.getInputStream());
			gpsManager = new GPSManager(
					(ArrayList<GPS>) inputStream.readObject());

			socket.close();
			
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}

		return gpsManager;
	}
	
	
	public GPSManager receiveMemberLocation(Event currentEvent) {
		GPSManager GPSLog = new GPSManager();
		
		try {
			Socket socket = new Socket(HOST, PORT);

			ObjectOutputStream outStream = new ObjectOutputStream(
					socket.getOutputStream());
			outStream.writeObject("RequestMemberCurrLocation/");
			outStream.flush();

			outStream.writeObject(currentEvent);
			outStream.flush();
			

			ObjectInputStream inputStream = new ObjectInputStream(
					socket.getInputStream());
			GPSLog = (GPSManager) inputStream.readObject();
			socket.close();
			
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}

		return GPSLog;
	}
}